//
//  GenericSettingsViewController.swift
//  SitPosition
//
//  Created by apple on 2019/1/1.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import UIKit
import Cartography


/**
 * 通用设置页面
 */
class GenericSettingsViewController : GKNavigationBarViewController {
    
    // 标题
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.Color.primaryDarkTextColor
        label.font = Theme.Font.boldFont(ofSize: 26)
        label.numberOfLines = 1
        label.lineBreakMode = .byTruncatingTail
        label.text = Strings.genericSettings
        return label
    }()
    
    // 列表
    lazy var tableView: UITableView = {
        let table = UITableView(frame: CGRect.zero, style: UITableView.Style.plain)
        table.separatorColor = Theme.Color.primaryDarkSeperatorColor
        table.separatorInset = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
        table.rowHeight = 90
        table.dataSource = self
        table.delegate = self
        table.register(SettingTableViewCell.self, forCellReuseIdentifier: SettingTableViewCell.reusedIdentifier)
        return table
    }()
    
    // 退出登录
    lazy var logoutButton: UIButton = {
        let btn = UIButton(type: UIButton.ButtonType.custom)
        btn.layer.cornerRadius = 5
        btn.layer.borderWidth = 1
        btn.layer.borderColor = Theme.Color.warningColor.cgColor
        btn.setTitleColor(Theme.Color.warningColor, for: UIControl.State.normal)
        btn.titleLabel?.font = Theme.Font.regularFont(ofSize: 16)
        btn.setTitle(Strings.genericSettingLogout, for: UIControl.State.normal)
        btn.addTarget(self, action: #selector(_logout), for: UIControl.Event.touchUpInside)
        return btn
    }()
    
    // MARK: - Object lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 导航栏
        gk_statusBarStyle = .default
        gk_navBackgroundColor = UIColor.clear
        gk_navLeftBarButtonItem = UIBarButtonItem(imageName: "ic_nav_back_black", target: self, action: #selector(_backButtonTapped))
        
        // 子视图
        view.addSubviews([
            titleLabel,
            tableView,
            logoutButton
        ])
        _setupContraints()
        view.backgroundColor = UIColor.white
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return UIStatusBarStyle.default
    }
}

// MARK: - UITableViewDataSource/UITableViewDelegate

extension GenericSettingsViewController : UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SettingTableViewCell.reusedIdentifier,
                                                 for: indexPath) as! SettingTableViewCell
        switch indexPath.row {
        case 0:
            cell.iconIV.image = UIImage(named: "tool_password")
            cell.titleLabel.text = Strings.genericSettingSetPassword
        case 1:
            let recommendCode = UserManager.manager.user?.recommendCode ?? "N/A"
            cell.iconIV.image = UIImage(named: "tool_code")
            cell.titleLabel.text = Strings.genericSettingMyInvitCode.replacingOccurrences(of: "$INVITCODE", with: recommendCode)
        case 2:
            cell.iconIV.image = nil
            cell.titleLabel.text = Strings.aboutUs
        case 3:
            cell.iconIV.image = nil
            cell.titleLabel.text = Strings.contactUs
        default: fatalError()
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        // 响应点击逻辑
        switch indexPath.row {
        case 0:
            // 修改密码
            _changePassword()
        case 1:
            // 查看收益
            _showIncoming()
        case 2:
            // 关于我们
            _showAboutUs()
        case 3:
            // 联系我们
            _contactUs()
        default:
            break
        }
    }
}

extension GenericSettingsViewController {
    
    // 设置视图约束
    func _setupContraints() {
        // titleLabel
        constrain(titleLabel, gk_navigationBar, view) { v, sibling, parent in
            v.top == sibling.bottom + 19
            v.left == parent.left + 16
            v.right == parent.right - 16
            v.height == 25
        }
        
        // tableView
        constrain(tableView, titleLabel, view) { v, sibling, parent in
            v.top == sibling.bottom
            v.left == parent.left
            v.right == parent.right
            v.bottom == parent.bottom - 75
        }
        
        // logoutButton
        constrain(logoutButton, view) { v, parent in
            v.left == parent.left + 15
            v.right == parent.right - 15
            if #available(iOS 11.0, *) {
                v.bottom == parent.safeAreaLayoutGuide.bottom - 20
            } else {
                v.bottom == parent.bottom - 15
            }
            v.height == 50
        }
    }
    
    // 返回按钮点击事件
    @objc func _backButtonTapped() {
        if presentingViewController != nil {
            dismiss(animated: true, completion: nil)
        } else {
            navigationController?.popViewController(animated: true)
        }
    }
    
    // 修改密码
    func _changePassword() {
        let vc = UserFindPasswordViewController()
        vc.backCallback = { [weak self] in
            self?.navigationController?.popViewController(animated: true)
        }
        navigationController?.pushViewController(vc, animated: true)
    }
    
    // 查看收益
    func _showIncoming() {
        let vc = BenefitBoardViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    // 关于我们
    func _showAboutUs() {
        let vc = AboutViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    // 联系我们
    func _contactUs() {
        let vc = ContactUsViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    // 登出用户
    @objc func _logout() {
        // 登出用户
        UserManager.manager.logout()
    }
}

